<template>
	<div>
		<el-upload class="avatar-uploader" :action="apiurl+'api/admin/upload'" :show-file-list="false"
			:on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :headers="picheaders">
			<img v-if="pic" :src="pic" class="avatar">
			<i v-else class="el-icon-plus avatar-uploader-icon"></i>
		</el-upload>
		<span class="text-hui pl10" v-if="text!=0">建议尺寸：{{text}}</span>
	</div>
</template>

<script>
	export default {
		name: 'PicUp',
		props: {
			myIcon: String,
			myText: String,
		},
		data() {
			return {
				pic: this.myIcon,
				text: this.myText,
				picheaders:{},
				apiurl:'',
			}
		},
		created() {
			var that = this;
			console.log(that.myIcon)
			var token = that.common.GetStorage('bytoken')
			that.picheaders = {
				'X-Requested-With': 'XMLHttpRequest',
				'Authorization': 'Bearer ' + token,
			}
			that.apiurl = that.common.CommLink();
		},
		methods: {
			handleAvatarSuccess(res, file) {
				console.log(res.data.path);
				this.pic = res.data.path;
				this.$emit('handleChange', this.pic);
			},
			beforeAvatarUpload(file) {
				console.log(file.type);
				if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/jpeg') {
					const isLt2M = file.size / 1024 / 1024 < 1;
					if (!isLt2M) {
						this.$message.error('图片大小不能超过 1MB!');
					}
				} else {
					this.$message.error('图片只支持png,jpg格式');
				}
				// return isJPG && isLt2M;
			},
		}
	}
</script>
